using M13.Base;
using M13.GameSystem.Inventory;
using QFramework;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using M13.Game.Item.Weapon;
using UnityEngine;

namespace M13.Game.UI
{
    public class HUDControler : MonoBehaviour, IController, ICanSendEvent
    {
        private HUD _hud;

        private void Awake()
        {
            
        }

        private void Start()
        {
            _hud = GetComponent<HUD>();
            this.GetModel<IPlayerModal>().SelectedShortcutSoltIndex.RegisterWithInitValue((e) => { ChangeSelectBoxPos(e); });
            this.GetModel<IPlayerModal>().PlayerInventory.Register((e) => { SetInventoryInfo(e); });

        }
        private void Update()
        {
            var i = this.GetModel<IPlayerModal>().PlayerInventory.Value;
            SetInventoryInfo(i);
        }

        /// <summary>
        /// ���ÿ��/��Ʒ������Ϣ
        /// </summary>
        /// <param name="inventory"></param>
        private void SetInventoryInfo(Inventory inventory)
        {
            for (int i = 0; i < inventory.CellCount; i++)
            {
                if (inventory.IsEmptyCell(i))
                {
                    _hud.SetShortcutSlot(i, null, 0, -1);
                    continue;
                }

                float norDurability = -1;
                if (inventory.GetItem(i) == null) Debug.Log($"��Ʒ����{i}Ϊ��");
                
                if (inventory.GetItem(i).ItemProperties.ItemType == Item.ItemType.Weapon)
                {
                    norDurability = (inventory.Cells[i].DurabilityValue / (float)(inventory.GetItem(i) as Weapon).WeaponProperties.MaxDurabilityValue);
                }
                _hud.SetShortcutSlot(i, inventory.GetItem(i).ItemProperties.ItemIcon, inventory.Cells[i].Count, norDurability);
            }
        }

        private void ChangeSelectBoxPos(int index)
        {
            _hud.SetSlotSelectBoxPos(index);
        }

        public IArchitecture GetArchitecture()
        {
            return GameArchitecture.Interface;
        }
    }
}